package com.qiyuesuo.auth.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qiyuesuo.auth.entity.User;
import com.qiyuesuo.auth.mapper.UserMapper;
import com.qiyuesuo.auth.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Random;

/**
 * @Author： xulinbo
 * @Description：
 * @Date： 16:44 2022/4/26
 **/
@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Override
    public String registerUser(UserVo userVo) {
        Random random = new Random();
        int id = random.nextInt();
        User user = new User();
        user.setId(id);
        BeanUtils.copyProperties(userVo,user);
        User login = userMapper.selectOne(new QueryWrapper<User>().eq("name", userVo.getName()));
        if (login != null){
            return "该账户已存在";
        }
        userMapper.insert(user);
        return "注册成功";
    }

    @Override
    public User login(UserVo userVo) {
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("name", userVo.getName()).eq("password", userVo.getPassword()));
        return user;
    }
}
